package day01;

/**
 * @Author Stringzhua
 * @Date 2024/10/29 11:35
 * description:
 */
public class Test10 {
    public static void main(String[] args) throws InterruptedException {
        System.out.println("主线程：开始执行");

        // main主线程创建子线程MyThread
        MyThread t = new MyThread();
        t.start();

        Thread.sleep(1000);
        t.interrupt(); // 中断t线程
        t.join(); // 等待t线程结束

        System.out.println("主线程：结束执行");
    }
}

class MyThread extends Thread {
    public void run() {
        System.out.println("MyThread线程：开始执行");

        // MyThread线程创建子线程HelloThread
        HelloThread hello = new HelloThread();
        hello.start(); // 启动HelloThread线程
        try {
            hello.join(); // 等待hello线程结束
        } catch (InterruptedException e) {
            System.out.println("MyThread线程：结束执行，interrupted!");
        }

        // MyThead线程结束后，中断子线程HelloThread
        hello.interrupt();
    }
}

class HelloThread extends Thread {
    public void run() {
        System.out.println("Hello线程：开始执行");
        int n = 0;
        while (!isInterrupted()) {
            n++;
            System.out.println(n + " hello!");
        }
        System.out.println("Hello线程：结束执行！");
    }
}
